/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller.app;

import com.yami.shop.bean.model.FaceCompareRecord;
import com.yami.shop.bean.model.User;
import com.yami.shop.common.exception.FaceCompareErrorException;
import com.yami.shop.common.util.baidu.face.FaceCompare;
import com.yami.shop.dao.UserLikeRecordMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.FaceCompareRecordService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/p/user")
@Api(tags = "用户接口")
@AllArgsConstructor
public class UserFaceCheckController {

    private final UserService userService;

    private final FaceCompareRecordService faceCompareRecordService;
    /**
     * 查看用户接口
     */
    @GetMapping("/faceVerify")
    @ApiOperation(value = "人脸识别", notes = "传入拍摄的照片，进行比对，返回非200表示错误")
    public ResponseEntity<String> verify(@Param("用户此次验证提交的照片路径") String submitPath) {
        String userId = SecurityUtils.getUser().getUserId();
        User one = userService.getById(userId);
        boolean b = false;
        String errmsg = "SUCCESS";
        FaceCompareRecord r = new FaceCompareRecord();
        try {
            b = FaceCompare.faceCompare(submitPath, one.getIdentityPic());
            r.setResult(b?1:2);
        } catch (Exception e) {
            if(e instanceof FaceCompareErrorException){
                errmsg = e.getMessage();
                r.setResult(2);
            }else{
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        }
        r.setUserId(userId);
        r.setCreateTime(new Date());
        r.setErrMsg(errmsg);
        r.setUserPic(one.getPic());
        r.setUserNickname(one.getNickName());
        r.setSubmitPic(submitPath);
        r.setOriginPic(one.getIdentityPic());
        r.setUserPhone(one.getUserMobile());
        faceCompareRecordService.save(r);

        if(!b){
            return ResponseEntity.badRequest().body("验证失败");
        }else{
            return ResponseEntity.ok("验证通过");
        }
    }



}
